package com.myMinistry.gui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.myMinistry.R;

public class ActionBarFragment extends Fragment {
	public static final int DIALOG_SELECT_PUBLISHERS = 1;
	
	private Button selectedPublisherName, add, entries, shareTime, save, saveAndAdd;
	private SharedPreferences sharedPref;
	private Activity currentActivity;
	
	public static ActionBarFragment newInstance() {
		ActionBarFragment f = new ActionBarFragment();
        return f;
    }
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.action_bar_fragment, container,true);
		currentActivity = getActivity();
		sharedPref = currentActivity.getSharedPreferences(Summary.MY_PREFS, Context.MODE_PRIVATE);
		
		/** Get the view elements. */
		selectedPublisherName = (Button) root.findViewById(R.id.sharedPrefName);
		shareTime = (Button) root.findViewById(R.id.shareTime);
		add = (Button) root.findViewById(R.id.add);
		entries = (Button) root.findViewById(R.id.entries);
		save = (Button) root.findViewById(R.id.save);
		saveAndAdd = (Button) root.findViewById(R.id.saveAndAdd);
		
		/** Has a default publisher been created - OR - Does a publisherID not exist */
		if(!sharedPref.getBoolean(Summary.MY_PREFS_KEY_CREATED,false) || sharedPref.getInt(Summary.MY_PREFS_KEY_PUBLISHER_ID, 0) == 0) {
			/** We need to take them to the introduction screen to create their first publisher */
			startActivity(new Intent(currentActivity.getApplicationContext(), GettingStarted.class));
			currentActivity.finish();
		}
		
		/** Default preferences exists - lets get their information */
		selectedPublisherName.setText(sharedPref.getString(Summary.MY_PREFS_KEY_PUBLISHER_NAME, currentActivity.getApplicationContext().getString(R.string.defaultPubName)));
		
		selectedPublisherName.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(currentActivity instanceof Summary)
					((Summary) currentActivity).showDialog();
				else if(currentActivity instanceof TimeEditor)
					((TimeEditor) currentActivity).showDialog();
				else if(currentActivity instanceof TimeEntries)
					((TimeEntries) currentActivity).showDialog();
				else if(currentActivity instanceof Householders)
					((Householders) currentActivity).showDialog();
				else if(currentActivity instanceof HouseholderEditor)
					((HouseholderEditor) currentActivity).showDialog();
				else if(currentActivity instanceof Publishers)
					((Publishers) currentActivity).showDialog();
				else if(currentActivity instanceof PublisherEditor)
					((PublisherEditor) currentActivity).showDialog();
				else if(currentActivity instanceof LiteratureManager)
					((LiteratureManager) currentActivity).showDialog();
				else if(currentActivity instanceof LiteratureEditor)
					((LiteratureEditor) currentActivity).showDialog();
			}
		});
		
		/** Only the Summary activity will see these options */
		if(currentActivity instanceof Summary) {
			shareTime.setVisibility(View.VISIBLE);
			shareTime.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					((Summary) currentActivity).shareTime();
				}
			});

			entries.setVisibility(View.VISIBLE);
			entries.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					((Summary) currentActivity).viewEntries();
				}
			});
		}
		
		/** Only the Time Editor and Householder Activities will have the add option. */
		if(currentActivity instanceof Householders || currentActivity instanceof Publishers || currentActivity instanceof LiteratureManager) {
			add.setVisibility(View.VISIBLE);
			add.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(currentActivity instanceof Householders)
						((Householders) currentActivity).createNewHouseholderShowDialog();
					else if(currentActivity instanceof Publishers)
						((Publishers) currentActivity).createNewPublisherShowDialog();
					else if(currentActivity instanceof LiteratureManager)
						((LiteratureManager) currentActivity).createNewLiterature();
				}
			});
		}
		
		/** Only the Householder Editor Activity will have the save option. */
		if(currentActivity instanceof HouseholderEditor || currentActivity instanceof TimeEditor || currentActivity instanceof PublisherEditor || currentActivity instanceof LiteratureEditor) {
			save.setVisibility(View.VISIBLE);
			save.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(currentActivity instanceof HouseholderEditor)
						((HouseholderEditor) currentActivity).save();
					else if(currentActivity instanceof TimeEditor)
						((TimeEditor) currentActivity).save();
					else if(currentActivity instanceof PublisherEditor)
						((PublisherEditor) currentActivity).save();
					else if(currentActivity instanceof LiteratureEditor)
						((LiteratureEditor) currentActivity).save();
				}
			});
		}
		
		/** Only the Time Editor Activity will have the add with save options. */
		if(currentActivity instanceof TimeEditor) {
			saveAndAdd.setVisibility(View.VISIBLE);
			saveAndAdd.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(currentActivity instanceof TimeEditor)
						((TimeEditor) currentActivity).showTypesOfLiterature();
				}
			});
		}
		
		return root;
    }
    
	public void setSelectedPublisherNameText(String name) {
		selectedPublisherName.setText(name);
	}
}